<template>
  <div>
    <div class="maincss">
      <el-row>
        <el-card>
          <el-card class="filter-container" shadow="never">
            <el-row class="searchbox">
              <el-col span="1"> 质控规则</el-col>
              <el-col span="3">
                <el-input
                  placeholder="质控品编号"
                  v-model="queryParams.materialNo"
                >
                </el-input>
              </el-col>
              <el-col span="3">
                <el-input
                  placeholder="质控品名称"
                  v-model="queryParams.materialName"
                >
                </el-input>
              </el-col>
              <el-col span="1">
                <el-button @click="handleLoadAndQuery" type="primary">查询</el-button>
              </el-col>
              <el-col span="2">
                <el-button @click="handleOpenAdd" type="primary">新增</el-button>
              </el-col>
            </el-row>

          </el-card>
          <el-card class="gray-card no-border no-margin-top" shadow="never">
            <el-table
              v-loading="loading"
              :data="checkList"
              style="width: 100%;"
            >

              <el-table-column label="质控品名称" align="center" prop="materialName" :show-overflow-tooltip="true"/>
              <el-table-column label="质控品编号" align="center" prop="materialNo" :show-overflow-tooltip="true"/>
              <el-table-column label="所属科室" align="center" prop="deptName" :show-overflow-tooltip="true"/>
              <el-table-column label="创建人" align="center" prop="createBy"/>
              <el-table-column label="修改时间" align="center" prop="createTime"/>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    @click="handleEdit(scope.row)"
                    v-hasPermi="['monitor:online:forceLogout']"
                  >编辑
                  </el-button>
                  <el-button
                    size="mini"
                    type="text"
                    @click="handleDetail(scope.row)"
                    v-hasPermi="['monitor:online:forceLogout']"
                  >详情
                  </el-button>
                  <el-button
                    size="mini"
                    type="text"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['monitor:online:forceLogout']"
                  >作废
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-card>
          <div class="pagination-container">
            <pagination v-show="total>0" :total="total"
                        :page.sync="queryParams.pageNum"
                        :limit.sync="queryParams.pageSize"
                        @pagination="handleLoadAndQuery"/>
          </div>
        </el-card>
      </el-row>
      <el-drawer
        class="no-head"
        :visible.sync="addShow"
        size="65%"
      >
        <qualityRuleDetail @close="handleClose"
                           :disabled="disabled"
                           :title="title"
                           ref="detailRef"/>
      </el-drawer>
    </div>
  </div>
</template>

<script>
import qualityRuleDetail from './detail.vue'
import {cancelById, listQualityRules} from '../../../api/business/qualityRules'
import {listQualityRulesItem} from "@/api/business/qualityRulesItem"

export default {
  name: 'maintenanceInfo',
  components: {
    qualityRuleDetail
  },
  data() {
    return {
      title: '新增',
      disabled: false,
      checkList: [],
      addShow: false,
      loading: false,
      redirect: undefined,
      activeName: '1',
      deviceInfo: {},
      status: [],
      inspectionRsult: [],
      value: '',
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        materialNo: '',
        materialName: ''
      }
    }
  },
  watch: {},
  created() {
    this.handleLoadAndQuery()
  },
  methods: {
    handleOpenAdd() {
      this.addShow = true
      this.disabled = false
      this.title = '新增'
    },
    handleLoadAndQuery() {
      listQualityRules(this.queryParams).then(response => {
        this.checkList = response.rows
      })
    },
    handleEdit(row) {
      this.addShow = true
      this.disabled = false
      this.title = '编辑'
      this.$nextTick(() => {
        if (row.id) {
          listQualityRulesItem({
            ruleId: row.id,
            pageNum: 1,
            pageSize: 100
          }).then((res) => {
            this.$refs.detailRef.handleData(row, res.rows)
          })
        }
      })
    },
    handleDetail(row) {
      this.addShow = true
      this.disabled = true
      this.title = '详情'
      this.$nextTick(() => {
        if (row.id) {
          listQualityRulesItem({
            ruleId: row.id,
            pageNum: 1,
            pageSize: 100
          }).then((res) => {
            this.$refs.detailRef.handleData(row, res.rows)
          })
        }
      })
    },
    handleDelete(row) {
      cancelById(row.id).then((res) => {
        this.$message.success('操作成功')
        this.handleLoadAndQuery()
      })
    },
    handleClose() {
      this.addShow = false;
      this.disabled = false
      this.handleLoadAndQuery()
    }
  }
}
</script>
